import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import 'package:smart_translator/app/event/DataCallBack.dart';
import 'package:smart_translator/app/modules/home/controllers/home_controller.dart';
import 'package:smart_translator/app/routes/app_pages.dart';
import 'package:smart_translator/app/services/AudioService.dart';
import 'package:smart_translator/app/style/app_colors.dart';
import 'package:smart_translator/app/style/widget_style.dart';
import 'package:smart_translator/app/utils/CommonUtil.dart';
import '../../db/DbDAOHelper.dart';
import '../../db/database.dart';
import '../../utils/tips.dart';
import '../../widgets/AppHeader.dart';
import '../../widgets/CommonWidgets.dart';

class TranscriptViewView extends GetView<HomeController> {
  const TranscriptViewView({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Obx(()=>Stack(
          children: [
            Container(
              child: SvgPicture.asset(
                "assets/images/header_part_bg.svg",
                width: Get.width,
              ),
            ),
            AppHeader.obtainHeaderForRecordScript(),
            Container(
              width: Get.width,
              margin: EdgeInsets.only(top: 90.h),
              padding: EdgeInsets.all(20.w),
              child: Column(
                children: [
                  SizedBox(height: 10.h,),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          InkWell(
                            onTap: () {
                              controller.openDrawer();
                            },
                            child: Container(
                              padding: EdgeInsets.all(8.w),
                              decoration: BoxDecoration(
                                boxShadow: [
                                  CommonWidgets.getBoxShadow(),
                                ],
                                color:Colors.white,
                                borderRadius: BorderRadius.circular(8), // Optional: add border radius
                              ),
                              child: Image.asset('assets/images/icon_recordscript_leftwindow.png',width: 16.w,height: 16.h,),
                            ),
                          ),
                          SizedBox(width: 7.w,),
                          Text('${controller.folderNameChecked.value}(${controller.sessionCount.value})',style: AppStyleThemeData.RECORD_SCRIPT_TEXT_STYLE)
                        ],
                      ),
                      controller.folderNameChecked.value == '回收站'?
                      InkWell(
                        onTap: () {
                           controller.clearRabbish();
                        },
                        child: Container(
                          padding: EdgeInsets.only(left: 10.w,right: 10.w,bottom: 5.w,top: 5.w),
                          child: Image.asset('assets/images/icon_rubish_recycle_btn.png',width: 16.w,height: 16.h,),
                        ),
                      ):SizedBox()
                    ],
                  ),
                  SizedBox(height: 12.h,),
                  Expanded(child: controller.sessionList.isEmpty ? Container(
                          child: Column(
                            children: [
                              SizedBox(height: 150.h,),
                              Image.asset('assets/images/empty_list_realtime.png',width: 169.w,height: 91.h),
                              SizedBox(height: 16.h,),
                              Text('暂无录音文件',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                            ],
                          ),
                     ) : ListView.builder(itemBuilder: (context, index) {
                        Session sessionItem = controller.sessionList.value[index];
                        return GestureDetector(
                          onTap: () {
                            DbDAOHelper().updateSessionHasReadById(sessionItem.id, true);
                            Get.toNamed(Routes.RECORDSCRIPT_DETAIL,parameters: {
                              'sessionId': sessionItem.id.toString(),
                              'audioType': 'old'
                            });
                          },
                          onLongPress: () {
                             Tips.showToast('long press...');
                          },
                          child: Column(
                            key: ValueKey(sessionItem.id),
                            children: [
                              Container(

                                padding: EdgeInsets.only(left: 15.w,right: 15.w,top: 13.h,bottom: 15.h),
                                decoration: BoxDecoration(
                                  boxShadow: [
                                    CommonWidgets.getBoxShadow(),
                                  ],
                                  color:Colors.white,
                                  borderRadius: BorderRadius.circular(8), // Optional: add border radius
                                ),
                                child: Column(
                                  mainAxisSize: MainAxisSize.min,
                                  children: [
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        Text('${sessionItem.session_name}',style: AppStyleThemeData.RECORD_SCRIPT_TEXT_STYLE),
                                        sessionItem.has_read ? SizedBox(): Container(
                                          width: 5.w,
                                          height: 5.h,
                                          decoration: BoxDecoration(
                                              shape: BoxShape.circle,
                                              color:Colors.red// Optional: add border radius
                                          ),
                                          child: SizedBox(),
                                        )
                                      ],
                                    ),
                                    SizedBox(height: 10.h,),
                                    Container(
                                      child: Row(
                                        children: [
                                          Image.asset('assets/images/icon_record_script_canlender.png',width: 14.w,height: 14.h,),
                                          SizedBox(width: 5.w,),
                                          Text('${CommonUtil.formatCustomWeekday(sessionItem.create_time)}',style: AppStyleThemeData.HINT_TEXT_STYLE_small,),
                                          SizedBox(width: 16.w,),
                                          Image.asset('assets/images/icon_record_script_clock.png',width: 14.w,height: 14.h,),
                                          Text(CommonUtil.formatMillisecondsToHHMMSS2(sessionItem.audio_length),style: AppStyleThemeData.HINT_TEXT_STYLE_small),
                                        ],
                                      ),
                                    ),
                                    SizedBox(height: 10.h,),
                                    Row(
                                      children: [
                                        Text('NOTE',style: AppStyleThemeData.HINT_TEXT_STYLE_small)
                                      ],
                                    )
                                  ],
                                ),
                              ),
                              SizedBox(height: 20.h)
                            ],
                          ),
                        );
                  },
                  itemCount: controller.sessionList.length,
                  padding: EdgeInsets.all(0),
                  ),flex: 1,)
                ],
              ),
            )
          ],
        )),
        floatingActionButton: InkWell(
          onTap: () {
            controller.createSession(DataCallBack(
              businessCallBack: (v) {
                AudioService audioService = Get.find<AudioService>();
                if(audioService.isPlaying.value) {
                  Tips.showToast('正在录音中,请先关闭录音后尝试.');
                  return;
                } else {
                  Get.toNamed(Routes.RECORDSCRIPT_DETAIL,parameters: {
                    'sessionId': v.toString(),
                    'audioType': 'new'
                  });
                }

              }
            ));
          },
          child: Container(
            width: 48.w,
            height: 48.h,
            decoration: BoxDecoration(
              shape: BoxShape.circle
            ),
            child: SvgPicture.asset('assets/images/icon_record_script_recording_btn_sggg.svg'),
          ),
        ),


    );
  }
}
